package com.cqc.paoclient.ui.code;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cqc.paoclient.R;
import com.cqc.paoclient.adapter.CodeRvAdapter;
import com.cqc.paoclient.base.BaseFragment;
import com.cqc.paoclient.model.CodeModel;
import com.cqc.paoclient.ui.code.codedetail.CodeDetailActivity;
import com.cqc.paoclient.ui.search.SearcherActivity;
import com.cqc.paoclient.util.LogUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by cui on 2017/11/22.
 */

public class Tab2Frag extends BaseFragment<Tab2Presenter> implements Tab2Contract.View, OnRefreshListener, OnLoadmoreListener {


    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    private List<CodeModel.ItemsBean> list;
    private CodeRvAdapter adapter;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_tab_2;
    }

    @Override
    protected Tab2Presenter getmPresenter() {
        return new Tab2Presenter();
    }

    @Override
    protected void presenterSetView() {
        if (mPresenter != null) {
            mPresenter.setView(this);
        }
    }

    @Override
    protected void initView() {
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        adapter = new CodeRvAdapter(list);
        recyclerView.setAdapter(adapter);
        adapter.setmItemClickListener(new CodeRvAdapter.ItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(getContext(), CodeDetailActivity.class);
                intent.putExtra("id", list.get(position).getId());
                intent.putExtra("title", list.get(position).getTitle());
                startActivity(intent);
            }
        });
    }

    @Override
    protected void loadData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.tab_2_frag, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.app_bar_search:
                startActivityWithIntent(SearcherActivity.class);
                break;
        }
        return true;
    }

    @Override
    public void getCodeListSuccess(CodeModel codeModel) {
        tvNoData.setVisibility(View.GONE);
        stopRefresh(refreshLayout);
        list.clear();
        list.addAll(codeModel.getItems());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getCodeListFail(String text) {
        showToat(text);
        stopLoadMore(refreshLayout);
        stopRefresh(refreshLayout);
    }

    @Override
    public void loadMoreSuccess(CodeModel codeModel) {
        tvNoData.setVisibility(View.VISIBLE);
        stopLoadMore(refreshLayout);
        int index = list.size();
        list.addAll(codeModel.getItems());
//        adapter.notifyDataSetChanged();
        adapter.notifyItemRangeInserted(index, codeModel.getItems().size());
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getCodeList();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        mPresenter.loadMoreList();
    }
}
